package com.shangcheng.server.controller;

import com.shangcheng.model.domain.PmsProductCategory;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.PmsProductCategoryVo;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService productCategoryService;

    /**
     * 分页查询商品分类
     *
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @return
     */
    @GetMapping("/list/{parentId}")
    public Result list(Integer pageNum, Integer pageSize, @PathVariable("parentId") Integer parentId) {
        PageResult result = productCategoryService.list(pageNum, pageSize, parentId);
        return new Result(200, result, "操作成功");
    }

    /**
     * 查询所有一级分类及子分类
     *
     * @return
     */
    @GetMapping("/list/withChildren")
    public Result withChildren() {
        List<PmsProductCategoryVo> list = productCategoryService.withChildren();
        return new Result(200, list, "操作成功");
    }

    /**
     * 添加商品分类
     *
     * @param productCategoryVo
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody PmsProductCategoryVo productCategoryVo) {
        productCategoryService.create(productCategoryVo);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 修改商品分类
     *
     * @param id
     * @param productCategoryVo
     * @return
     */
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody PmsProductCategoryVo productCategoryVo) {
        productCategoryService.update(id, productCategoryVo);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 修改显示状态
     *
     * @param params
     * @return
     */
    @PostMapping("/update/showStatus")
    public Result showStatus(@RequestParam Map params) {
        productCategoryService.showStatus(params);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 修改导航栏显示状态
     *
     * @param params
     * @return
     */
    @PostMapping("/update/navStatus")
    public Result navStatus(@RequestParam Map params) {
        productCategoryService.navStatus(params);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        PmsProductCategory productCategory = productCategoryService.findById(id);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        r.setData(productCategory);
        return r;
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        productCategoryService.deleteById(id);
        Result r = new Result();
        r.setCode(200);
        r.setMessage("操作成功");
        return r;
    }

}
